var _v;
var vueModel = {
    el: '.main',
    data: {
        Moment: moment,
        protocol: false,
        room_type_id: '',
        params: {
            hotel_id: '',
            room_id: '',
            in_date: '',
            out_date: '',
            contract_member: '',
            contract_mobile: '',
            room_number: '',
            stay_persons: [],
            dis_self_sn: ''
        },
        breakfast: '',
        total_price: '',
        orderId: '',
        orderInfo: {

        },
        hotelDetail: {
            images: {
                list: [
                    {
                        imgid: ''
                    }, {
                        imgid: ''
                    }, {
                        imgid: ''
                    }, {
                        imgid: ''
                    }, {
                        imgid: ''
                    }
                ]
            },
        },
        rules: {
            dis_self_sn: [
                { required: true, message: '订单号必填！', trigger: 'blur' },
            ]
        }
    },
    computed: {
        roomType: function() {
            switch (parseInt(_v.room_type_id)) {
                case 1:
                    return '标准房'
                    break;
                case 2:
                    return '高级房'
                    break;
                case 3:
                    return '豪华房'
                    break;
                case 4:
                    return '商务房'
                    break;
                case 5:
                    return '行政房'
                    break;
                case 6:
                    return '双人房'
                    break;
                case 7:
                    return '单人房'
                    break;
                case 8:
                    return '豪华套房'
                    break;
            }
        }
    },
    methods: {
        submitOrder: function() {
            if (!_v.protocol) {
                this.$message({
                    type: 'error',
                    message: '请查看并同意服务条款及预订协议！'
                })
                return;
            }
            if (!_v.orderId) {
                HQ.ajax("/room/order/done", JSON.stringify(_v.params), function(res){
                    _v.$message({
                        message: '提交成功！',
                        type: 'error'
                    })
                    _v.orderId = res.data.hotel_order_id;
                    window.open('/order_wait.html' + location.search + '&orderId=' + _v.orderId, '_blank');
                }, function(data){
                    _v.$message({
                        message: data.msg,
                        type: 'error'
                    })
                }, "POST");
            } else {
                _v.params.hotel_order_id = _v.orderId;
                HQ.ajax("/room/order/update_book", JSON.stringify(_v.params), function(res){
                    _v.$message({
                        message: '提交成功！',
                        type: 'error'
                    })
                    window.open('/order_wait.html' + location.search + '&orderId=' + _v.orderId, '_blank');
                }, function(data){
                    _v.$message({
                        message: data.msg,
                        type: 'error'
                    })
                }, "POST");
            }

        },
        getArea: function () {
            HQ.getArea(function(data) {
                _v.cityList = data.data;
            })
        },
        getQueryVariable: function (variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] == variable){return pair[1];}
            }
            return(false);
        },
        fetchData: function () {
            HQ.ajax('/room/order/pre_book', JSON.stringify({
                date_start: _v.params.in_date,
                date_end: _v.params.out_date,
                room_number: _v.params.room_number,
                room_id: _v.params.room_id,
            }), function(res){
                if (res.code === 0) {
                    _v.breakfast = res.data.breakfast;
                    _v.total_price = res.data.total_price;
                }
            }, function(){}, "POST");
        },
        fetchData2: function () {
            HQ.ajax("/hotel/info", JSON.stringify({
                hotel_id: _v.params.hotel_id
            }), function(res){
                if (res.code === 0) {
                    _v.hotelDetail = res.data;
                }
            }, function(data){

            }, "POST");
        },
        getOrderInfo: function() {
            HQ.ajax("/room/pre_booking_info", JSON.stringify({
                hotel_order_id: _v.orderId
            }), function(res){
                _v.params = res.data;
                _v.fetchData();
                _v.fetchData2();
                _v.params.stay_persons = res.data.stay_persons.slice(0, _v.params.room_number)
            }, function(data){

            }, "POST");
        },
    },
    created: function() {
        _v = this;
    },
    mounted: function () {
        _v.orderId = _v.getQueryVariable('orderId');
        _v.room_type_id = _v.getQueryVariable('room_type_id') || '';

        if(_v.orderId) {
            _v.getOrderInfo();
        } else {
            _v.params.in_date = _v.getQueryVariable('date_start') || '2020-05-06';
            _v.params.out_date = _v.getQueryVariable('date_end') || '2020-05-07';
            _v.params.room_id = _v.getQueryVariable('room_id') || '';
            _v.params.room_number = _v.getQueryVariable('room_number') || '';
            _v.params.hotel_id = _v.getQueryVariable('hotel_id') || '';
            _v.orderId = _v.getQueryVariable('orderId');
            _v.fetchData();
            _v.fetchData2();
            for (var i = 0; i < _v.params.room_number; i++) {
                _v.params.stay_persons.push({person_name:"",person_mobile:""})
            }
        }
    }
}

$(function() {
    new Vue(vueModel);
})